package com.busines.dao.mep.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.common.util.QPReflectionUtils;

@Repository("baseDao")
public class BaseDao<T,E> extends SqlSessionDaoSupport{
	
	private Class<T> entityClass;

	
	public int deleteByPrimaryKey(Integer id) {
		return this.getSqlSession().delete(this.getQueryPath("deleteByPrimaryKey"), id);
	}

	public int insert(T entity) {
		return this.getSqlSession().insert(this.getQueryPath("insert"), entity);
	}

	public int insertSelective(T entity) {
		return this.getSqlSession().insert(this.getQueryPath("insertSelective"), entity);
	}

	@SuppressWarnings("unchecked")
	public T selectByPrimaryKey(Integer id) {
		return (T)this.getSqlSession().selectOne(this.getQueryPath("selectByPrimaryKey"),id);
	}

	public int updateByPrimaryKeySelective(T entity) {
		return this.getSqlSession().update(this.getQueryPath("updateByPrimaryKeySelective"), entity);
	}

	public int updateByPrimaryKey(T entity) {
		return this.getSqlSession().update(this.getQueryPath("updateByPrimaryKey"), entity);
	}
	
	public int countByExample(E example) {
		return this.getSqlSession().selectOne(this.getQueryPath("countByExample"),example);
	}

	public int deleteByExample(E example) {
		return this.getSqlSession().delete(this.getQueryPath("deleteByExample"),example);
	}

	public List<T> selectByExample(E example) {
		return this.getSqlSession().selectList(this.getQueryPath("selectByExample"),example);
	}

	public int updateByExampleSelective(T record, E example) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("record", record);
		map.put("example", example);
		return this.getSqlSession().update(this.getQueryPath("updateByExampleSelective"), map);
	}

	public int updateByExample(T record, E example) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("record", record);
		map.put("example", example);
		return this.getSqlSession().update(this.getQueryPath("updateByExample"), map);
	}
	
	
	public List<T> selectByExampleWithBLOBs(E example){
		return this.getSqlSession().selectList(this.getQueryPath("selectByExampleWithBLOBs"),example);
	}
	
	public int updateByExampleWithBLOBs(T record, E example){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("record", record);
		map.put("example", example);
		return this.getSqlSession().update(this.getQueryPath("updateByExampleWithBLOBs"), map);
	}
	
	public int updateByPrimaryKeyWithBLOBs(T record){
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("record", record);
		return this.getSqlSession().update(this.getQueryPath("updateByPrimaryKeyWithBLOBs"), map);
	}
	
	
	@Resource(name="sqlSessionFactory")
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {  
        super.setSqlSessionFactory(sqlSessionFactory);  
    }
	
	public BaseDao(){
		/*
		 * 通过反射拿到传进来的实体信息
		 */
		this.entityClass = QPReflectionUtils.getSuperClassGenricType(getClass());
	}
	/**
	 * 命名空间路径
	* @Title: getNampSpace 
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @author feilun 
	* @param @return
	* @return String
	* @throws
	 */
	protected String getQueryPath(String mapperid)
    {
		/*
		 * 获取实体名称 拼接Dao以及mapper对应的sql id
		 * 所以需要Mapper命名空间=实体名称+Dao，并且配置为dao目录下
		 */
		String querypath = entityClass.getName() + "Dao." + mapperid;
        return querypath.replace("pojo", "dao");
    }
}
